package com.mukefile.action;

import com.mukefile.pojo.Manager;
import com.mukefile.pojo.User;
import com.mukefile.service.ManagerService;
import com.mukefile.utils.ContextUtil;
import com.mukefile.utils.constant.Constant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: kenewstar
 * @Description:
 * @Date:Created in 2020/5/6
 */
@Controller("managerAction")
@Scope("prototype")
public class ManagerAction extends ActionSupport implements ModelDriven<Manager> {
    @Resource
    private ManagerService managerService;
    private final Manager manager = new Manager();
    @Override
    public Manager getModel() {
        return this.manager;
    }

    /**
     * 管理员用户登录
     * @return 页面
     */
    public String managerLogin(){
        //md5加密
        this.manager.setPwd(new Md5Hash(this.manager.getPwd()
                , Constant.SALT).toString());
        Manager manager = this.managerService.managerLogin(this.manager);
        ContextUtil.getSessionMap().put("manager",manager);
        if (manager==null){
            ContextUtil.getRequestMap().put("msg","用户名或密码错误！");
            return "login";
        }
        return "main";
    }

    /**
     * 管理员退出
     * @return
     */
    public String managerExit(){

        this.managerService.managerExit(ContextUtil.getSessionMap());
        return "login";
    }

    /**
     * 显示所有用户信息
     * @return 返回所有用户信息
     */
    public String showAllUser(){

        List<User> users = this.managerService.showAllUser();
        if (users.isEmpty()){
            ContextUtil.getRequestMap().put("msg","暂无用户");
        }
        ContextUtil.getRequestMap().put("list",users);
        return "showAllUser";
    }
    private Integer uid;
    public Integer getUid() {
        return uid;
    }
    public void setUid(Integer uid) {
        this.uid = uid;
    }
    /**
     * 重置用户密码
     * @return 页面
     */
    public String resetPwd(){
        // 获取管理员信息
        Manager manager = (Manager)ContextUtil.getSessionMap().get("manager");

        Integer uid = this.uid;
        int i = this.managerService.resetPwd(uid,manager);
        if (i==0){
            ContextUtil.getRequestMap().put("msg","密码重置失败");
        }else {
            ContextUtil.getRequestMap().put("msg","密码重置成功");
        }
        ContextUtil.getRequestMap().put("list",this.managerService.showAllUser());
        return "resetPwd";
    }
    private Integer size;
    public Integer getSize() {
        return size;
    }
    public void setSize(Integer size) {
        this.size = size;
    }

    /**
     * 磁盘扩容
     * @return 页面
     */
    public String expandDisk(){
        // 获取管理员信息
        Manager manager = (Manager)ContextUtil.getSessionMap().get("manager");
        Integer uid = this.uid;
        Integer size = this.size;
        int i = this.managerService.expandDisk(uid, size,manager);

        return "expand";
    }

    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * 文件查询操作
     * @return 页面
     */
    public String searchUser(){
        List<User> users = this.managerService.searchUser(this.username);
        if (users.isEmpty()){
            ContextUtil.getRequestMap().put("msg","查无此用户");
        }
        ContextUtil.getRequestMap().put("list",users);
        return "showAllUser";
    }

    /**
     * 查询用户是否存在
     */
    public void selectUser() {
        HttpServletResponse resp = ContextUtil.getResponse();
        try {
            List<User> users = this.managerService.selectUserByName(username);
            if (users.isEmpty()){
                resp.getWriter().write("");
            }else {
                //用户已存在
                resp.getWriter().write(users.get(0).getUsername());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 添加新用户
     * @return 页面
     */
    public String addUser(){
        // 获取管理员信息
        Manager manager = (Manager)ContextUtil.getSessionMap().get("manager");
        // 调用添加用户方法
        User user = new User();
        user.setUsername(this.username);
        List<User> users = this.managerService.selectUserByName(this.username);
        if (!users.isEmpty()){
            ContextUtil.getRequestMap().put("msg","请勿重复添加");
            ContextUtil.getRequestMap().put("list",this.managerService.showAllUser());
            return "showAllUser";
        }
        int i = this.managerService.addUser(user,manager);
        if (i==0){
            //创建失败
            ContextUtil.getRequestMap().put("msg","用户添加失败");
        }else {
            ContextUtil.getRequestMap().put("msg","用户添加成功");
        }
        ContextUtil.getRequestMap().put("list",this.managerService.showAllUser());
        return "showAllUser";
    }

    public String updatePwd(){
        return "";
    }
}
